package cn.sofwin.orm;

import cn.sofwin.orm.entity.User;
import cn.sofwin.orm.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

@Slf4j
public class Main {

    public static void main(String[] args) {
        String resource = "mybatis-config.xml";

        // sqlSessionFactory 会话，不要关闭
        SqlSessionFactory sqlSessionFactory = null;
        SqlSession sqlSession = null;

        try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession();

            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = userMapper.selectOne(17);
            System.out.println(user);
            log.warn("用户信息：{}", user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
